import { Component, OnInit } from '@angular/core';
import { AuditingComponent } from '../../auditing/auditing.component';
import { MyModalHelper } from '@core/my-modal.helper';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import { IntegralConvertService } from '@core/routes-services/integral-convert.service';
import { LEVEL_APPLICATION_OPTION } from '../../../constant/dictionary';
import { IntegralConvertViewComponent } from '../convert-view/convert-view.component';

@Component({
  selector: 'app-integral-integral-convert',
  templateUrl: './integral-convert.component.html',
})
export class IntegralIntegralConvertComponent implements OnInit {


  isLoading = false;
  keyword: any;
  status: any;
  statusOptions: any [] = LEVEL_APPLICATION_OPTION;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };


  constructor(private message: NzMessageService,
              private router: Router,
              private integralConvertService: IntegralConvertService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }


  initPage() {
    this.isLoading = true;
    this.integralConvertService.pagination(this.page, this.keyword, this.status).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }


  seeDetail = (entity) => {
    let temp: boolean;
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(IntegralConvertViewComponent, { entity: bak }, 800, {
      nzTitle: '申请详情',
    }).subscribe(result => {
      if (result) {
        this.message.success('驳回成功');
        this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };
  reject = (entity) => {
    let temp: boolean;
    const bak = {};
    Object.assign(bak, entity);
    bak['remark'] = bak['rejectReason'];
    this.modalHelper.open(AuditingComponent, { entity: bak, businessService: this.integralConvertService }, 360, {
      nzTitle: '驳回兑换请求',
    }).subscribe(result => {
      if (result) {
        this.message.success('驳回成功');
        this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  agree = (entity) => {
    this.integralConvertService.auditing(entity.id, { status: 2 }).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.message.success('操作成功');
        this.changePageNo(this.page.number);
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  };

}
